﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace 水牛管理系统.Utils {
    class DatebaseUtil {
        private MySqlConnection conn;

        public DatebaseUtil() {
        }

        ~DatebaseUtil() {
            close();
        }

        public MySqlConnection open() {
            if (conn == null) conn = new MySqlConnection(DatebaseUtil.GetConStr);
            if (conn.State == System.Data.ConnectionState.Closed) {
                try {
                    conn.Open();
                    return conn;
                }
                catch (MySqlException ex) {
                    switch (ex.Number) {
                        case 0:
                            MessageBox.Show("Cannot connect to server.  Contact administrator", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
                            break;
                        case 1045:
                            MessageBox.Show("Invalid username/password, please try again", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
                            break;
                    }
                }
            }
            return null;
        }

        public void close() {
            if (conn.State == System.Data.ConnectionState.Open) conn.Close();
        }

        public static string GetConStr {
            get {
               // return "server=106.12.111.47;port=3366;user=Visitor;password=123456; database=VisitorMS;";
                return "server=106.12.111.47;port=3366;user=root;password=white1@lu; database=water_card;";
            }
        }
    }
}
